/**
 * Taro 页面分享 装饰器
 * @param opts
 * @return {function(*): WithOptions}
 */
function withOptions(opts = {}) {

  return function demoComponent(Component) {

    class WithOptions extends Component {

      async componentWillMount() {

        if (super.componentWillMount) {
          super.componentWillMount()
        }

      }

      async componentDidShow() {

        if (super.componentDidShow) {
          super.componentDidShow()
        }

      }

      // 点击分享的那一刻会进行调用
      onShareAppMessage() {
        let {
          title = '', //主标题
          desc = '', //简介
          path = '', //分享链接
          imageUrl = '', //小程序 logo
          bgImgUrl = '', //当前页面主图，为空取当前页面截图
        } = opts

        // 从继承的组件获取配置
        if (this.$setSharePath && typeof this.$setSharePath === 'function') {
          path = this.$setSharePath()
        }

        // 从继承的组件获取配置
        if (this.$setShareTitle && typeof this.$setShareTitle === 'function') {
          title = this.$setShareTitle()
        }

        // 从继承的组件获取配置
        if (this.$setShareImageUrl && typeof this.$setShareImageUrl === 'function') {
          imageUrl = this.$setShareImageUrl()
        }

        // 从继承的组件获取配置
        if (this.$setShareBgImgUrl && typeof this.$setShareBgImgUrl === 'function') {
          bgImgUrl = this.$setShareBgImgUrl()
        }

        // 从继承的组件获取配置
        if (this.$setShareDesc && typeof this.$setShareDesc === 'function') {
          desc = this.$setShareDesc()
        }

        // 如果path不带参数，分享出去后解析的params里面会带一个{''： ''}
        // const sharePath = `${path}`

        console.log({
          title,
          path,
          desc,
          imageUrl,
          bgImgUrl
        })
        return {
          title,
          path,
          desc,
          imageUrl,
          bgImgUrl
        }
      }

      render() {
        return super.render();
      }

    }

    return WithOptions;
  };
}

export default withOptions;

